package com.cloudcc.mobile.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import butterknife.Bind;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.map.MapLocation;
import com.cloudcc.mobile.manager.LocationManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.customer.CustomerActivity;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;

public class MapsActivity extends BaseActivity implements
		OnTitleBarClickListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	@Bind(R.id.bmapView)
	MapView mMapView; // 地图View
	BaiduMap mBaiduMap;
	private BDLocation lastDBLocation;
	private InfoWindow mInfoWindow;
	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;

	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		initHeader();
		initMap();
	}

	private void initHeader() {
		headerbar.setOnTitleBarClickListener(this);
		onLocationing();
	};

	private void onLocationing() {
		headerbar.beginProgress();
		if ("en".equals(mEns)) {
			headerbar.setTitle("Is positioning...");
			}else{
				headerbar.setTitle("正在定位...");
			}
		
	}

	private void onLocationFinish() {
		headerbar.completeProgress();
		if ("en".equals(mEns)) {
			headerbar.setTitle("I am here");
			}else{
				headerbar.setTitle("我在这里");
			}
		
	}

	private void initMap() {
		mBaiduMap = mMapView.getMap();
		mMapView.showZoomControls(false);
		mBaiduMap.setMyLocationEnabled(true);
		BitmapDescriptor bitmap = BitmapDescriptorFactory
				.fromResource(R.drawable.icon_marka);
		MyLocationConfiguration config = new MyLocationConfiguration(
				LocationMode.NORMAL, true, bitmap);
		mBaiduMap.setMyLocationConfigeration(config);
		LocationManager.getInstance().registerListener(mListener);
		LocationManager.getInstance().requestLocal();
		// mMapView = (MapView) findViewById(R.id.bmapView);
		// mBaiduMap = mMapView.getMap();
		//
		// //普通地图
		// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

		// 卫星地图
		// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);

		// 空白地图,
		// 基础地图瓦片将不会被渲染。在地图类型中设置为NONE，将不会使用流量下载基础地图瓦片图层。使用场景：与瓦片图层一起使用，节省流量，提升自定义瓦片图下载速度。
		// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);
	}

	@Override
	protected void onStop() {
		super.onStop();
		LocationManager.getInstance().unregisterlistener(mListener);
	}

	@Override
	public void onDestroy() {
		try {
			mMapView.onDestroy();
		} catch (Exception e) {
			Tools.handle(e);
		}
		super.onDestroy();
	}

	/**
	 * 显示
	 * 
	 * @param lat
	 */
	private void showMarkOverlay(BDLocation lat) {
		LatLng ll = new LatLng(lat.getLatitude(), lat.getLongitude());
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 17.0f);
		mBaiduMap.animateMapStatus(u);
		mBaiduMap.clear();
		TextView viewContent = (TextView) View.inflate(mContext,
				R.layout.map_make_item, null);
		viewContent.setText(lat.getAddrStr() + "\n"
				+ StringUtils.remove(lat.getLocationDescribe(), "在"));
		mInfoWindow = new InfoWindow(viewContent, ll, 35);
		mBaiduMap.showInfoWindow(mInfoWindow);
	}

	@Override
	public void onPause() {
		mMapView.onPause();
		super.onPause();
	};

	@Override
	public void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	public int getLayoutId() {
		return R.layout.map;
	}

	private BDLocationListener mListener = new BDLocationListener() {
		@Override
		public void onReceiveLocation(BDLocation location) {
			onLocationFinish();
			if (null != location
					&& location.getLocType() != BDLocation.TypeServerError) {
				lastDBLocation = location;
				LocationManager.getInstance().stopRequestLocal();
				LocationManager.log(lastDBLocation);
				showMarkOverlay(lastDBLocation);
			} else {
				if (null != location) {
					if ("en".equals(mEns)) {
						showToast("Positioning failed, please try again! Error code:" + location.getLocType());
						}else{
							showToast("定位失败，请重试! 错误码:" + location.getLocType());
						}
					
				}
			}
		}
	};

	@Override
	public void onClickLeft(View v) {
		finish();
	}

	@Override
	public void onClickRight(View v) {
		String qd = "签到拜访";
		String kh = "客户";
		
		if (lastDBLocation == null) {
			if ("en".equals(mEns)) {
				 qd = "Sign in to visit";
				 kh = "client";
				showToast("Pro, yet accurate positioning please wait, is accurate positioning...");
				}else{
					showToast("亲，暂未准确定位请稍等,正在精准定位中...");
					 qd = "签到拜访";
					 kh = "客户";
				}
			
			return;
		}
		MapLocation locations = new MapLocation();
		locations.setAddress(LocationManager.getDetailAddr(lastDBLocation,
				false));
		locations.setLatitude(lastDBLocation.getLatitude());
		locations.setLongitude(lastDBLocation.getLongitude());
		String tag = getIntent().getStringExtra("beauinfo");
		String name = getIntent().getStringExtra("name");
		String id = getIntent().getStringExtra("id");
		if ("beau".equals(tag)) {
			Intent intent = new Intent(mContext, EditorActivity.class);
			if ("en".equals(mEns)) {
				intent.putExtra("CustomerActivity", "Sign in to visit " + "【" + name + "】");
				Log.d("CustomerActivity", "client " + "【" + name + "】" + 2);
				intent.putExtra("urlId", id);
				intent.putExtra("reuevantType", "Sign in to visit");
				intent.putExtra("MapsActivity", locations);
				intent.putExtra("key", "0");
				intent.putExtra("from", "map");
				intent.putExtra("name", "client:" + name);
				}else{
					intent.putExtra("CustomerActivity", qd + "【" + name + "】");
					Log.d("CustomerActivity", kh + "【" + name + "】" + 2);
					intent.putExtra("urlId", id);
					intent.putExtra("reuevantType", qd);
					intent.putExtra("MapsActivity", locations);
					intent.putExtra("key", "0");
					intent.putExtra("from", "map");
					intent.putExtra("name", kh+":" + name);
				}
			
			startActivity(intent);
		} else {

			Intent intent = new Intent(MapsActivity.this,
					CustomerActivity.class);
			intent.putExtra("MapsActivity", locations);
			startActivity(intent);
		}
	}
}
